package com.threeonemeeting.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.util.MiscUtil;
import com.weibo.sina.OAuthConstant;
import com.weibo.sina.Weibo;
import com.weibo.sina.WeiboException;
import com.weibo.tencent.QWeiboSyncApi;

public class ShareMeetingActivity extends BaseTabActivity {

	private String strShareTo = "";

	protected ProgressDialog loadingDialog;

	private Handler uiHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				loadingDialog.dismiss();
				break;

			// 分享新浪微博成功
			case 1:
				loadingDialog.dismiss();
				MessageBox.showToast(
						ShareMeetingActivity.this.getApplicationContext(),
						"成功分享至新浪微博");
				back();
				break;

			// 分享新浪微博失败
			case 2:
				loadingDialog.dismiss();
				MessageBox.showToast(
						ShareMeetingActivity.this.getApplicationContext(),
						"会议分享至新浪微博失败");
				back();
				break;

			// 分享腾讯微博成功
			case 3:
				loadingDialog.dismiss();
				MessageBox.showToast(
						ShareMeetingActivity.this.getApplicationContext(),
						"成功分享至腾讯微博");
				back();
				break;

			// 分享腾讯微博失败
			case 4:
				loadingDialog.dismiss();
				MessageBox.showToast(
						ShareMeetingActivity.this.getApplicationContext(),
						"会议分享至腾讯微博失败");
				back();
				break;
			}
		}
	};

	private void shareToSina(String strContent) {

		// 检查是否已经绑定
		String strSinaWeiboToken = MiscUtil.getPrefer("SinaWeiboToken");
		if (MiscHelper.isEmpty(strSinaWeiboToken) == false) {
			try {
				Weibo weibo = OAuthConstant.getInstance().getWeibo();
				weibo.setOAuthAccessToken(MiscUtil.getPrefer("SinaWeiboToken"),
						MiscUtil.getPrefer("SinaWeiboSecret"));
				String finalMsg = URLEncoder.encode(strContent)
						+ URLEncoder.encode("-来自\"31会议网\"应用程序的分享", "UTF-8");
				weibo.updateStatus(finalMsg);
				uiHandler.sendEmptyMessage(1);

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				uiHandler.sendEmptyMessage(2);
			} catch (WeiboException e) {
				e.printStackTrace();
				uiHandler.sendEmptyMessage(2);
			}
		}
	}

	private void shareToQQ(String strContent) {

		String strQQWeibo = MiscUtil.getPrefer("QQWeiboToken");
		if (MiscHelper.isEmpty(strQQWeibo) == false) {

			QWeiboSyncApi qWeibo = new QWeiboSyncApi();
			try {

				String finalMsg = strContent + "-来自\"31会议网\"应用程序的分享";
				String strResult = qWeibo
						.publishMsg(
								DataCenter.QQ_CONSUMER_KEY,
								DataCenter.QQ_CONSUMER_SECRET,
								MiscUtil.getPrefer("QQWeiboToken"),
								MiscUtil.getPrefer("QQWeiboSecret"),
								finalMsg,
								null,
								com.weibo.tencent.QWeiboType.ResultType.ResultType_Json);

				JSONObject jsonObject = new JSONObject(strResult);
				int nRet = jsonObject.getInt("ret");
				if (nRet == 0) {
					uiHandler.sendEmptyMessage(3);
				} else {
					uiHandler.sendEmptyMessage(4);
				}

			} catch (JSONException e) {
				e.printStackTrace();
				uiHandler.sendEmptyMessage(4);
			}
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_share_meeting);
		init("会议分享");

		System.setProperty("weibo4j.oauth.consumerKey",
				DataCenter.SINA_CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",
				DataCenter.SINA_CONSUMER_SECRET);

		Bundle b = getIntent().getExtras();
		String strTitle = b.getString("title");
		String strUrl = b.getString("url");
		strShareTo = b.getString("to");

		Button btnShare = (Button) findViewById(R.id.btn_submit);
		if (btnShare != null) {
			if (strShareTo.equalsIgnoreCase("sina") == true) {
				btnShare.setText("立即分享到新浪微博");
			} else if (strShareTo.equalsIgnoreCase("qq") == true) {
				btnShare.setText("立即分享到腾讯微博");
			}
		}

		// 帐号编辑框
		EditText editShareContent = (EditText) findViewById(R.id.editSharingContent);
		final String strContent = "我在31会议网发现一个有用的会议，" + strTitle + "，" + strUrl
				+ "，推荐给大家";
		editShareContent.setText(strContent);

		Button btnSubmit = (Button) findViewById(R.id.btn_submit);
		btnSubmit.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (strShareTo.equalsIgnoreCase("sina") == true) {
					loadingDialog = MessageBox.showProcessDlg(
							ShareMeetingActivity.this, "正在分享至新浪微博，请稍候...");
				} else if (strShareTo.equalsIgnoreCase("qq") == true) {
					loadingDialog = MessageBox.showProcessDlg(
							ShareMeetingActivity.this, "正在分享至腾讯微博，请稍候...");
				}
				loadingDialog.show();

				Runnable runable = new Runnable() {
					public void run() {
						if (strShareTo.equalsIgnoreCase("sina") == true) {
							shareToSina(strContent);
						} else if (strShareTo.equalsIgnoreCase("qq") == true) {
							shareToQQ(strContent);
						}
					}
				};

				new Thread(runable).start();
			}
		});
	}
}
